<?php

namespace App\Modules\Admin\Entities\Mall;

use App\Models\Model;
use App\Modules\Admin\Http\Controllers\Mall\GoodsRuleExtendModel;

class GoodsModel extends Model
{

    protected $table="goods";
    protected $primaryKey="goods_id";
    protected $is_delete=0;



    public function getSearchList($where,$params)
    {
//        if (isset($where['star'])) $with[] = 'star';
//        return $this->where($where)->with($with)
//            ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
//            $query->whereIn('id', function ($query) use ($where) {
//                $query->name('store_product_cate')->where('cate_id', is_array($where['sid']) ? 'in' : '=', $where['sid'])->field('product_id')->select();
//            });
//        })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
//            $query->whereIn('id', function ($query) use ($where) {
//                $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
//                    $query->name('store_category')->where('pid', is_array($where['cid']) ? 'in' : '=', $where['cid'])
//                        ->whereOr('id', is_array($where['cid']) ? 'in' : '=', $where['cid'])->field('id')->select();
//                })->field('product_id')->select();
//            });
//        })->when(isset($where['coupon_category_id']) && $where['coupon_category_id'] != '', function ($query) use ($where) {
//            $where['coupon_category_id'] = stringToIntArray($where['coupon_category_id']);
//            $query->whereIn('id', function ($query) use ($where) {
//                $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
//                    $query->name('store_category')->whereIn('pid', $where['coupon_category_id'])->field('id')->select();
//                })->whereOr('cate_id', 'in', $where['coupon_category_id'])->field('product_id')->select();
//            });
//        })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
//            if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
//                $query->whereIn('id', $where['ids']);
//            } else {
//                $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
//            }
//        })->when(isset($where['is_live']) && $where['is_live'] == 1, function ($query) use ($where) {
//            $query->whereNotIn('id', function ($query) {
//                $query->name('live_goods')->where('is_del', 0)->where('audit_status', '<>', 3)->field('product_id')->select();
//            });
//        })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
//            if ($where['priceOrder'] === 'desc') {
//                $query->order("price desc");
//            } else {
//                $query->order("price asc");
//            }
//        })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
//            if ($where['newsOrder'] === 'news') {
//                $query->order("id desc");
//            }
//        })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
//            if ($where['salesOrder'] === 'desc') {
//                $query->order("sales desc");
//            } else {
//                $query->order("sales asc");
//            }
//        })->when(!isset($where['ids']), function ($query) use ($where) {
//            if (isset($where['timeOrder']) && $where['timeOrder'] == 1) {
//                $query->order('id desc');
//            } else if (isset($where['is_best']) && $where['is_best'] == 1) {
//                $query->order('sales desc,sort desc');
//            } else {
//                $query->order('sort desc,id desc');
//            }
//        })->when(!$page && $limit, function ($query) use ($limit) {
//            $query->limit($limit);
//        })->order('sort desc')->field($field)
//
//        ->paginate($this->getLimit($limit ?? 10));

    }
    public function cate_info()
    {
        return $this->hasOne(GoodsCateModel::class,'cate_id','cate_id');

    }
    public function content_info()
    {
        return $this->hasOne(GoodsContentModel::class,'goods_id','goods_id');

    }
    public function extend_info()
    {
        return $this->hasMany(GoodsRuleExtendModel::class,'goods_id','goods_id');

    }
    public function rule_info()
    {
        return $this->hasOne(GoodsRuleModel::class,'goods_id','goods_id')->select(['id','goods_id','rule_name','rule_value']);

    }

    public function updateGoodsInfo($goods_id,$update)
    {
        return $this->where(['goods_id'=>$goods_id])->update($update);
    }
    public function getGoodsInfo($where)
    {
        return $this->where($where)->first();
    }
    public function getGoodsInfoByGoodsIds($goodsIds)
    {
        return $this->whereIn($this->primaryKey,$goodsIds)->first();
    }
    public function getGoodsListByGoodsIds($goodsIds)
    {
        return $this->whereIn($this->primaryKey,$goodsIds)->where(['is_show'=>1])->get()->toArray();
    }
    public function getGoodsInfoByGoodsId($goodsId)
    {
        return $this->detail($goodsId);
    }


}
